﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZoneFiveSoftware.Common.Data;
using ZoneFiveSoftware.Common.Data.Fitness;
using ZoneFiveSoftware.Common.Data.GPS;
using ZoneFiveSoftware.Common.Visuals;
using ZoneFiveSoftware.Common.Data.Measurement;

using Dimebrain.TweetSharp.Fluent;
using Dimebrain.TweetSharp.Extensions;

using BENBUN.Utils;

namespace BENBUN.UI.SendTweet
{
    public partial class Tweet : Form
    {
        private IActivity _activity = null;
        private int _twitterCharLimit = int.Parse(BENBUN.Properties.Resources.TwitterLimit);

        public Tweet()
        {
            InitializeComponent();

            groupBox1.Text = Resources.StringResources.buttonGroup;
            buttonStartDate.Text = Resources.StringResources.buttonStartDate;
            buttonStartTime.Text = Resources.StringResources.buttonStartTime;
            buttonTime.Text = Resources.StringResources.buttonTime;
            buttonDistance.Text = Resources.StringResources.buttonDistance;
            buttonSpeedPace.Text = Resources.StringResources.buttonPace;
            showTwitterLimit();
        }

        private void showTwitterLimit()
        {
            int tweetLength = Settings.Settings.TwitterTweetCapacity - tweetbox1.Text.Trim().Length ;

            label1.Text = tweetLength.ToString() + " characters left";

            if (tweetLength >= 0)
            {
                label1.ForeColor = Color.Black;
                buttonTweet.Enabled = true;
            }
            else
            {
                label1.ForeColor = Color.Red;
                buttonTweet.Enabled = false;
            }
        }


        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //private void tweetbox1_TextChanged(object sender, EventArgs e)
        //{
        //    showTwitterLimit();
        //}

        public IActivity activity
        {
            set
            {
                _activity = value;

                if (_activity != null)
                {
                    buttonStartTime.Enabled = _activity.HasStartTime;
                    Speed.Units speed = _activity.Category.SpeedUnits;
                    if (speed == Speed.Units.Pace)
                    {
                        buttonSpeedPace.Text = "Pace";
                    }
                    else
                    {
                        buttonSpeedPace.Text = "Speed";
                    }
                }
            }
        }

        private void addToTweet(string textPart)
        {

            if (!tweetbox1.Text.EndsWith(" "))
            {
                tweetbox1.Text = tweetbox1.Text + " ";
            }

            tweetbox1.Text = tweetbox1.Text + textPart;
            tweetbox1.Focus();
        }

        private void buttonStartDate_Click(object sender, EventArgs e)
        {
            addToTweet(_activity.StartTime.ToLocalTime().ToShortDateString());
        }

        private void buttonStartTime_Click(object sender, EventArgs e)
        {
            if (_activity.HasStartTime)
            {
                addToTweet(_activity.StartTime.ToLocalTime().ToShortTimeString());
            }
        }

        private void buttonTime_Click(object sender, EventArgs e)
        {
            ActivityInfo info = ActivityInfoCache.Instance.GetInfo(_activity);
            addToTweet(TextUtils.toDurationText(info.Time));
        }

        private void buttonDistance_Click(object sender, EventArgs e)
        {
            ActivityInfo info = ActivityInfoCache.Instance.GetInfo(_activity);
            Length.Units distanceUnits = _activity.Category.DistanceUnits;

            addToTweet(Length.ToString(Length.Convert(info.DistanceMeters, Length.Units.Meter, distanceUnits), distanceUnits, "N" + Length.DefaultDecimalPrecision(distanceUnits).ToString() + "u"));

        }

        private void buttonSpeedPace_Click(object sender, EventArgs e)
        {
            Speed.Units speed = _activity.Category.SpeedUnits;
            Length.Units distanceUnits = _activity.Category.DistanceUnits;
            ActivityInfo info = ActivityInfoCache.Instance.GetInfo(_activity);

            string newTweet = TextUtils.ToSpeedText(info.DistanceMeters, info.Time, speed, distanceUnits, false);
            newTweet = newTweet + " (" + TextUtils.SpeedUnits(speed, distanceUnits) + "):";
            addToTweet(newTweet);
        }

        private void buttonTweet_Click(object sender, EventArgs e)
        {
            buttonTweet.Enabled = false;
            string user = Settings.Settings.TwitterScreenName;
            string password = Settings.Settings.TwitterPassword;
            string message = tweetbox1.Text + " " + Settings.Settings.TwitterTotalTags;

            var twitter = FluentTwitter.CreateRequest()
                .AuthenticateAs(user, password)
                .Statuses().Update(message).AsJson();

            Console.WriteLine(twitter.ToString());

            var response = twitter.Request();

            if (response.ExceptionStatus.ToString() != "Success")
            {
                MessageBox.Show(response.Exception.Message, "Twitter Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            var status = response.AsStatus();

            this.Close();
        }

        private void tweetbox1_TextChanged_1(object sender, EventArgs e)
        {
            showTwitterLimit();
        }

    }
}
